<?php
import("data.db.content.DbSet");
import("data.logic.file.FileInfo");

class DbImageSet extends DbSet {
	
	public $className = "data.logic.content.ImageSet";
	const CLASS_NAME = "data.logic.content.ImageSet";
	//
	// SQL Table description section
	//
	const TB_TABLENAME	= "pd_image_set";
	const TB_I			= 'iset';
	
	const TB_TITLE		= 'iset_title';
	const TB_DATE		= 'iset_date';
	
	public static function _get_TABLENAME() {return self::TB_TABLENAME;}
	public static function _get_ID() {return self::TB_I."_id";}
	public static function _get_SUFFIX() {return self::TB_I;}
	//
	//
	function backwardForeignCollection() {
		return array("Item"=>"data.logic.content.ImageSetItem");
	}
	
	// properties
	public $title;
	
	public function __construct($noinit=false) {
		parent::__construct($noinit);
	}
	
	function setItems(&$items) {
		$this->backwardForeigns["Item"]->setList($items);
	}
	function getItems() {
		return $this->backwardForeigns["Item"]->getList();
	}
	
	// restore
	function fillEntity($arrResult) {
		DbAbstractTable::fillEntity($arrResult);
		$this->setTitle($arrResult[self::TB_TITLE]);
	}
	
	//store
	function store() {
		DbAbstractTable::store();
		
		DbAbstractTable::__executeQuery("delete from ".ImageSetItem::TB_TABLENAME." where iseti_id_iset='".$this->getId()."'");
		
		foreach ($this->getItems() as $item) {
			$item->store();
		}
	}
	
	function fillFields() {
		DbAbstractTable::fillFields();
		$this->fildsForStore[self::TB_TITLE] = "'".$this->getTitle()."'";
		$this->fildsForStore[self::TB_DATE] = "'".date("Y-m-d H:i:s")."'";
	}
	
	
	function getImages() {
		return DbAbstractTable::__selectObjects(
			"select pd_file_info.* from pd_file_info where fi_id in (select iseti_id_fi from pd_image_set_item where iseti_id_iset='".$this->getId()."')",
			FileInfo::CLASS_NAME);
	}
	
	// same for all classes
	public static function findAll() {return DbAbstractTable::findAll(self::CLASS_NAME);}
    public static function findById($id) {return DbAbstractTable::findById($id, self::CLASS_NAME);}
}
?>